<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="author" content="hexinyang">	
	<title>售卡换卡</title>
	<script language="javascript" type="text/javascript" src="../script/print.js"></script>
	<script language="javascript" src="../script/common.js"  type="text/javascript" ></script>
	<script language="javascript" src="../script/card.js" type="text/javascript" ></script>
	<script type="text/javascript" >	
		//页面加载方法	
		var badCardType;
		var billBack = false;
		function load(){			
			Eui.showNavigation();
			setPages(["detview","readcardview"]);
			CardMgnt.cardUser.show("userinfo_div");
			badCardType = getPara("badCardType");
			var asn = getPara("asn");
			if (asn)
				oldCardForm.oldAsn.value = asn;
			initActiveX();
		}
		//查询显示信息明细
		function searchCard(){
			if(!checkForm(oldCardForm))return;			
			var asn = oldCardForm.oldAsn.value;
			DWR.call("mgntQueryService.checkChangeBadCard",asn,function(data){
				var ct = data.cardType;
				oldCardForm.cardType.value = ct.id
				oldCardForm.oldCardTypeName.value = ct.name;
				oldCardForm.oldAsn.readOnly = true;
				$("queryOldCardBtn").disabled = true;
				$("readNewCardBtn").disabled = false;
				CardMgnt.cardUser.show("userinfo_div",data.cardUser.id);
			});
		}
		
		//重置方法
		function onCancel(){
			if (badCardType && billBack){
				document.location.href = badCardType
			}else{
				badCardType = null;
				switchPage("readcardview");
				CardMgnt.resetForm(oldCardForm)
				oldCardForm.reset();
				newCardForm.reset();
				CardUserViewForm.reset();
				
				oldCardForm.oldAsn.readOnly = false;
				oldCardForm.oldAsn.className = "";
				oldCardForm.readBtn.disabled = false;
				
				oldCardForm.oldAsn.readOnly = false;
				$("changeCardBtn").disabled = true;
				$("readNewCardBtn").disabled = true;
				$("queryOldCardBtn").disabled = false;
			}
		}
		
		//读新卡
		function onReadCard(){
			var asn;
			try{
				getCard();
				asn = getAsn();
			}catch(e){showError(e.description);return;}
			
			newCardForm.newAsn.value = asn;
			
			DWR.call("mgntQueryService.checkInitedCard",asn,function(data){
				if (oldCardForm.cardType.value != data.cardType.id){showWarning("新旧卡片类型不一致！");return;}
				newCardForm.newCardTypeName.value = data.cardType.name;
				$("changeCardBtn").disabled = false;
			});
		}
		//读取坏卡
		function onReadBadCard(){
			onCancel();
			var asn;
			try{
				getCard();
				asn = getAsn();
			}catch(e){showError(e.description);return;}
			oldCardForm.oldAsn.value = asn;
			oldCardForm.oldAsn.readOnly = true;
			oldCardForm.oldAsn.className = "readonly";
			oldCardForm.readBtn.disabled = true;
		}
		//显示补卡凭证
		function showPrintView(logId){
			switchPage("detview");
			//alert("凭证待实现")
			Eui.getBillHtml("billview",{
				queryName:"change_issue_badcard_bill",
				logId:logId
			});
		}
		
		//换卡方法
		function changeCard(){
			var oldAsn = oldCardForm.oldAsn.value;
			var newAsn = newCardForm.newAsn.value;
			if (!CardMgnt.checkCardAsn(newAsn)) return;
			DWR.call("cardMgntService.changeIssueBadCard",oldAsn,newAsn,function(data){
				var str = "换卡成功！<br>新卡卡号为："+newAsn;
				showInformation(str);
				showPrintView(data.logId);
				try{
					WebPrint.print("billview");
				}catch(e){
					showError("打印单据出错:"+e.description);
				}
				while(true){
					try{
						CardMgnt.syncCardData(newAsn);
						break;
					}catch(e){
						if (showQuestion("同步卡数据失败！是否重试？<br>错误："+e.description)) continue;
						
						if(showQuestion("同步卡数据失败！是否换卡？")){
							CardMgnt.badCard.gotoPage(newAsn,badCardType);
						}
						return;
					}
				}
				if (data.cardUser.usePin){
					CardMgnt.startCardPin(data.card.asn);
				}
				billBack = true;
			},{blockScreen:true});
		}
	</script>
</head>
<body onload="load()" onunload="releaseCard()">
	<!--页面导航-->
	<div class="nav"><div id="nav">&nbsp;</div></div>
	<div id="readcardview">
		<div class="form">
			<form id="oldCardForm" name="oldCardForm">
				<input type="hidden" name="cardType">
				<div class="formTitle">卡片信息</div>
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>坏卡卡号</label> <input type="text" name="oldAsn" onblur="checkV(this,'asn',true)"><span class="des">*</span>
							<button name="readBtn" onclick="onReadBadCard()">读 卡</button>
						</td>
						<td><label>坏卡类型</label> <input type="text" name="oldCardTypeName" readonly class="readonly"></td>
					</tr>
				</table>
			</form>
			<form id="newCardForm" name="newCardForm">
				<input type="hidden" name="cardType">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
					<tr>
						<td><label>新卡卡号</label> <input type="text" name="newAsn" readonly class="readonly">&nbsp;&nbsp;</td>
						<td><label>新卡类型</label> <input type="text" name="newCardTypeName" readonly class="readonly"></td>
					</tr>
				</table>
			</form>
		</div>
		<div id="userinfo_div" class = "form">
		</div>
		<p>
			<button id="queryOldCardBtn" onclick="searchCard()">查询旧卡</button>
			<button id="readNewCardBtn" onclick="onReadCard()" disabled>读 新 卡</button>
			<button id="changeCardBtn" onclick="changeCard()" disabled>换 卡</button>
			<button onclick="onCancel()">重 置</button>
		</p>
	</div>
	
	<!--凭证页面-->
	<div id="detview" class="form" style="width:100%;display:none;">
		<div class="formTitle">
			<div class="l">售卡损坏卡凭证</div>
			<div class="r">
				<button onclick="WebPrint.preview('billview')">打印预览</button>
		 		<button onclick="WebPrint.print('billview')">打印</button>
		 		<button onclick="onCancel()">返 回</button>
			</div>
		</div>
		<div id="billview" align='center'></div>
	</div>
</body>
</html>